package com.giscloud.limp.service.impl;

import cn.hutool.core.date.DateUtil;
import com.giscloud.token.utils.TokenUserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.commons.model.Result;
import cn.hutool.core.collection.CollectionUtil;

import java.util.Date;
import java.util.List;
import com.giscloud.limp.entity.CompanyBlacklistExample;
import com.giscloud.limp.entity.dto.CompanyBlacklistDTO;
import com.giscloud.limp.dao.CompanyBlacklistDao;
import com.giscloud.limp.entity.CompanyBlacklist;
import com.giscloud.limp.service.CompanyBlacklistService;
import lombok.extern.log4j.Log4j2;


@Service("companyBlacklistService")
@Log4j2
public class CompanyBlacklistServiceImpl implements CompanyBlacklistService {

    @Autowired
    private TokenUserUtil tokenUserUtil;

    private final  CompanyBlacklistDao companyBlacklistDao;

    @Autowired
    public CompanyBlacklistServiceImpl(CompanyBlacklistDao companyBlacklistDao) {
        this.companyBlacklistDao = companyBlacklistDao;
    }
    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(CompanyBlacklistDTO companyBlacklistDTO) {
        log.debug("#queryPage parameter ={}", JSON.toJSONString(companyBlacklistDTO));
        PageHelper.startPage(companyBlacklistDTO.getPage(), companyBlacklistDTO.getRows());
        List<CompanyBlacklist> companyBlacklists = companyBlacklistDao.selectByExample(companyBlacklistDTO,tokenUserUtil.getOrgId());
        return PageInfo.of(companyBlacklists);
    }


    /**
     * 信息
     */
    @Override
    public CompanyBlacklist getById(Integer companyListId){
        log.debug("#getById parameter ={}", companyListId);
        return companyBlacklistDao.selectByPrimaryKey(companyListId);
    }

    /**
     * 保存
     */
    @Override

    public Result save(CompanyBlacklist companyBlacklist){
        log.debug("#save parameter ={}", JSON.toJSONString(companyBlacklist));
        companyBlacklist.setCreatedStaffId(tokenUserUtil.getUserId().intValue());
        companyBlacklist.setStatusCd(1);
        companyBlacklist.setCreatedDt(new Date());
        if(companyBlacklistDao.insertSelective(companyBlacklist) > 0 ){
            return Result.success("保存成功");
        }else{
            return Result.error("保存失败");
        }
    }

    /**
     * 修改
     */
    @Override

    public Result updateById(CompanyBlacklist companyBlacklist){
         log.debug("#updateById parameter ={}", JSON.toJSONString(companyBlacklist));
         CompanyBlacklistExample companyBlacklistExample = new CompanyBlacklistExample();
         CompanyBlacklistExample.Criteria criteria = companyBlacklistExample.createCriteria();
         criteria.andCompanyListIdEqualTo(companyBlacklist.getCompanyListId());
         companyBlacklist.setVersion(new Date());
        companyBlacklist.setOperateStaffId(tokenUserUtil.getUserId().intValue());
         if(companyBlacklistDao.updateByExampleSelective(companyBlacklist,companyBlacklistExample) > 0){
             return Result.success("修改成功");
         }else{
             return Result.error("修改失败");
         }
    }
    /**
     * 删除
     */
    @Override

    public Result removeByIds(List<Integer> companyListIds){
        log.debug("#removeByIds parameter ={}", JSON.toJSONString(companyListIds));
        CompanyBlacklistExample companyBlacklistExample = new CompanyBlacklistExample();
        CompanyBlacklistExample.Criteria criteria = companyBlacklistExample.createCriteria();

            criteria.andCompanyListIdIn(companyListIds);

        if(companyBlacklistDao.deleteByExample(companyBlacklistExample) > 0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

}